package com.xie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.domain.Result;
import com.xie.pojo.Appartus;
import com.xie.pojo.Bed;
import com.xie.pojo.Vo.RoomVo;
import com.xie.service.BedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "病床管理")
@RestController
@RequestMapping("/Beds")
public class BedController {
    @Autowired
    private BedService bedService;

    /**
     * 分页
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageIndex}/{pageSize}")
    @ApiOperation("获取分页病床数据")
    public Result getPage(@PathVariable Integer pageIndex,
                          @PathVariable  Integer pageSize){
        Page page=new Page(pageIndex,pageSize);
        IPage<Bed> page1 = bedService.page(page);
       if (page1 != null) return Result.ok(page1);
       return Result.fail();
    }

    /**
     * 模糊查询
     * @param params
     * @return
     */
    @GetMapping("find/{params}")
    @ApiOperation("模糊查询病床信息")
    public Result FindPage(@PathVariable String params){
        LambdaQueryWrapper<Bed> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Bed::getBedId,params)
                .or().like(Bed::getBedJudge,params)
                .or().like(Bed::getBedNo,params)
                .or().like(Bed::getBedRoomId,params)
        ;
        List<Bed> list = bedService.list(lambdaQueryWrapper);
        if (list != null) return Result.ok(list);
        return Result.fail();
    }

    /**
     * 添加
     * @param bed
     * @return
     */
    @ApiOperation("添加病床数据")
    @PostMapping("/save")
    public Result addBed(@RequestBody Bed bed) {
        boolean save = bedService.save(bed);
        if (!save) return Result.fail();
        return Result.ok();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除病床数据")
    public Result removeById(@PathVariable Long id){
        boolean b = bedService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 修改
     * @param bed
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改病床数据")
    public Result updateBed(@RequestBody Bed bed) {
        boolean update = bedService.updateById(bed);
        if (!update) return Result.fail();
        return Result.ok();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据id查询病床数据")
    public Result findBedById(@PathVariable Integer id) {
        Bed be2= bedService.getById(id);
        if (be2 != null) return Result.ok(be2);
        return Result.fail();
    }


    /**
     * 获取全部
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取所有病床数据")
    public Result exportBedlist(){
        List<Bed> beds = bedService.list();
        if (beds != null) return Result.ok(beds);
        return Result.fail();
    }


    /**
     * 病房病床查询
     * @return
     */
    @GetMapping(value = "/findRoomBed")
    @ApiOperation("/病房病床联表查询")
    public Result findRoomT() {
        List<RoomVo> beds = bedService.findRoomT();
        if (beds != null) return Result.ok(beds);
        return Result.fail();
    }
}
